package com.cdwh.joylife.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.blankj.utilcode.util.ScreenUtils;
import com.cdwh.joylife.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by admin on 2017/6/23.
 */

public class MeItemAdapter extends RecyclerView.Adapter<MeItemAdapter.MyViewHolder> {

    private int[] imagesId;
    private String[] texts;

    public MeItemAdapter(int[] imagesId, String[] texts) {
        this.imagesId = imagesId;
        this.texts = texts;
    }

    private OnItemClickLisenter onItemClickLisenter;

    public void setOnItemClickLisenter(OnItemClickLisenter onItemClickLisenter) {
        this.onItemClickLisenter = onItemClickLisenter;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.item_me, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.image.setImageResource(imagesId[position]);
        holder.name.setText(texts[position]);
        int screenWidth = ScreenUtils.getScreenWidth();
        double width = (double) screenWidth / (double) imagesId.length;
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) width, (int) width);
        holder.view.setLayoutParams(params);
    }

    @Override
    public int getItemCount() {
        return texts == null ? 0 : texts.length;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.image)
        ImageView image;
        @BindView(R.id.text)
        TextView name;
        @BindView(R.id.view)
        LinearLayout view;

        MyViewHolder(final View view) {
            super(view);
            ButterKnife.bind(this, view);

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickLisenter != null) {
                        onItemClickLisenter.onClick(view, getLayoutPosition());
                    }
                }
            });
        }
    }
}
